#include<cstdio>
#include<iostream>
#include<cstdio>
#include<string>
#include<fstream>
#include<vector>
#include<unordered_map>

using namespace std;

const string USER_INFO_FILE = "user.txt";



class TreeNode{
public:
    bool is_create = false;
    string Expression; //welcome 

    bool is_speak = false;
    vector<string> item; //存储speak

    bool is_listen = false;
    int beginTimer; //listen1
    int endTimer; //listen2

    //branch分支
    bool is_branch = false;
    unordered_map<string,string> HashTable;

    bool is_silence = false;
    string silence; 
    bool is_defaultProc = false;
    string defaultProc;

    //是否执行完退出
    bool is_Exit = false;
public:
    //打印speak
    void speakInfo(const string& _name,int _amount,bool isTest = false,const string& outFileName = "")
    {
        fstream ofile;
        //测试模块 将输出到out.txt文件以比对
        if(isTest){
            ofile.open(outFileName,ios::out | ios::app);
            if(!ofile.is_open()){
                cout << "opne file " << outFileName << "error" << endl;
                exit(-1);
            }
        }
        string ret = "";
        if(is_speak){
            for(auto vit = item.begin(); vit != item.end(); vit++){
                if(*vit == "$name"){
                    cout << _name;
                    if(isTest) ofile << _name;
                }else if(*vit == "$amount"){
                    cout << _amount;
                    if(isTest) ofile << _amount;
                }else{
                    cout << *vit;
                    if(isTest) ofile << *vit;
                }
            }
            cout << endl;
            if(isTest) ofile << endl;
        }
    }
    //打印信息
    void showData()
    {
        cout << "------------------------------------" << endl;
        cout << "expression:" << Expression << endl;
        
        if(is_speak){
            cout << "speak:";
            for(auto vit = item.begin(); vit != item.end(); vit++){
                cout << *vit;
            }
            cout << endl;
        }

        if(is_listen){
            cout << "listen:" << endl;
            cout << "start:" << beginTimer << endl;
            cout << "end:" << endTimer << endl;
        }
        
        if(is_branch){
            cout << "branch:" << endl;
            for(auto mit = HashTable.begin(); mit != HashTable.end(); mit++){
                cout << mit->first << " " << mit->second << endl;
            }
        }

        if(is_silence){
            cout << "silence:" << endl;
            cout << silence << endl;
        }
        
        if(is_defaultProc){
            cout << "defaultProc:" << endl;
            cout << defaultProc << endl;
        }

        if(is_Exit){
            cout << "Exit" << endl;
        }

    }
};

class Interpreter{
//测试使用的公有属性
public:
    int sleep_times = 1;
    int sleep_length = 10;
    vector<string> v_user_input;
    int input_ptr = 0;
    string outFilePath;
//私有属性
private:
    string m_filename;
    //当前用户信息
    string _name = "张三";
    int _amount = 100;
    //step集合
    string mainStep;
    //辅助变量,记录当前正在操作的step
    TreeNode curStep;
    //这个结构不合理
    vector<TreeNode> vStep;
    //map
    unordered_map<string,TreeNode> umapStep;
//构造析构    
public:
    Interpreter(string filename);
    ~Interpreter();
//功能模块
public:
    //按行读取
    void parseFile();
    //分析每行数据
    void parseLine(const string& line);
    //ProcessTokens
    void ProcessTokens(const vector<string>& token);
    //Step
    void ProcessStep(const string& expression);
    //Speak
    void ProcessSpeak(const vector<string>& token);
    //Expression
    void ProcessExpression(const string& s);
    //Listen
    void ProcessListen(int startTimer,int stopTimer);
    //Branch
    void ProcessBranch(const string& answer, const string& nextStepId);
    //Silence
    void ProcessSilence(const string& nextStepId);
    //Default
    void ProcessDefault(const string& nextStepId);
    //Error Exit
    void ProcessExit();
    //模块运行
    void InterpreterRunner(bool isTest = false);
//辅助模块
public:
    //判断当前行是否需要跳过分析
    bool isJumpOver(const string& line);
    //判断分隔符 ' ' ',' ';'
    bool isDelimiter(char c);
    //打印统计的单词
    void printWord(vector<string>& s);
    //打印构造的语法树
    void printTree(unordered_map<string,TreeNode>& umap);
    //给定名字 查找对应step的node
    TreeNode getNodeByName(const string& name);
    //询问分支匹配
    bool isMatch(const string& branchWord,const string& querySentence);
    //获取用户输入
    bool get_input(string& ret,int wTimer);
    //根据用户id获取用户名和用户账单
    void getUserInfo(int id);
};